﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Schema;
using CompletionData;
using System.Collections.Generic;

namespace CompletionDataTest
{
    [TestClass]
    public class DataManagerTest
    {
        #region GetXmlSchema
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetXmlSchemaTestMethod1()
        {
            CompletionData.DataManager.GetXmlSchema(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetXmlSchemaTestMethod2()
        {
            CompletionData.DataManager.GetXmlSchema("  ");
        }

        [TestMethod]
        public void GetXmlSchemaTestMethod3()
        {
            XmlSchema schema = CompletionData.DataManager.GetXmlSchema(@"C:\Temp\hoge.txt");
            Assert.IsNull(schema);
        }

        [TestMethod]
        public void GetXmlSchemaTestMethod4()
        {
            XmlSchema schema = CompletionData.DataManager.GetXmlSchema(@"C:\");
            Assert.IsNull(schema);
        }

        [TestMethod]
        public void GetXmlSchemaTestMethod5()
        {
            XmlSchema schema = CompletionData.DataManager.GetXmlSchema(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Commontypes.xsd");
            Assert.AreEqual(420, schema.Elements.Count);
        }
        #endregion

        #region GetMSBuildDirectoryPath
        [TestMethod]
        public void GetMSBuildDirectoryPathTestMethd1()
        {
            string result =CompletionData.DataManager.GetMSBuildShemaPath();
            Assert.AreEqual(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild\Microsoft.Build.Commontypes.xsd", result, true);
        }
        #endregion

        #region GetRootXmlSchemaElement
        [TestMethod]
        public void GetRootXmlSchemaElementTestMethod1()
        {
            XmlSchemaElement elem = CompletionData.DataManager.GetRootXmlSchemaElement();
            Assert.AreEqual("Project", elem.Name);
        }
        #endregion

        #region GetTask
        [TestMethod]
        public void GetTaskTestMethod1()
        {
            TaskInfo tinfo = DataManager.GetTask("/", "Project");
            Assert.IsNotNull(tinfo);
            Assert.AreEqual("Project", tinfo.Name);
            List<PropertyInfo> props = tinfo.Properties;
            Assert.AreEqual(3, props.Count);
            Assert.AreEqual("DefaultTargets", props[0].Name);
            Assert.AreEqual("InitialTargets", props[1].Name);
            Assert.AreEqual("ToolsVersion", props[2].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props[1].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props[2].Use);

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetTaskTestMethod2()
        {
            TaskInfo tinfo = DataManager.GetTask("/", null);

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetTaskTestMethod3()
        {
            TaskInfo tinfo = DataManager.GetTask("/", "");

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetTaskTestMethod4()
        {
            TaskInfo tinfo = DataManager.GetTask("", "aaa");

        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetTaskTestMethod5()
        {
            TaskInfo tinfo = DataManager.GetTask(null, "aaa");

        }

        [TestMethod]
        public void GetTaskTestMethod6()
        {
            TaskInfo tinfo = DataManager.GetTask("/Project", "Hoge");
            Assert.IsNull(tinfo);

        }

        [TestMethod]
        public void GetTaskTestMethod7()
        {
            TaskInfo tinfo = DataManager.GetTask("aaa", "Project");
            Assert.IsNull(tinfo);
        }

        [TestMethod]
        public void GetTaskTestMethod8()
        {
            TaskInfo tinfo = DataManager.GetTask("/Project", "ItemGroup");
            Assert.IsNotNull(tinfo);
            Assert.AreEqual("ItemGroup", tinfo.Name);
            List<PropertyInfo> props = tinfo.Properties;
            Assert.AreEqual(2, props.Count);
            Assert.AreEqual("Condition", props[0].Name);
            Assert.AreEqual("Label", props[1].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props[1].Use);
        }

        #endregion

        #region GetProperties
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetPropertiesTestMethod1()
        {
            DataManager.GetProperties("/", null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetPropertiesTestMethod2()
        {
            DataManager.GetProperties("/", "");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetPropertiesTestMethod3()
        {
            DataManager.GetProperties(null, "aa");
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetPropertiesTestMethod4()
        {
            DataManager.GetProperties("", "aa");
        }

        [TestMethod]
        public void GetPropertiesTestMethod5()
        {
            List<PropertyInfo> result = DataManager.GetProperties("/", "aa");
            Assert.AreEqual(0,result.Count);
        }

        [TestMethod]
        public void GetPropertiesTestMethod6()
        {
            List<PropertyInfo> props = DataManager.GetProperties("/", "Project");
            Assert.AreEqual(3, props.Count);
            Assert.AreEqual("DefaultTargets", props[0].Name);
            Assert.AreEqual("InitialTargets", props[1].Name);
            Assert.AreEqual("ToolsVersion", props[2].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props[1].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props[2].Use);

        }

        [TestMethod]
        public void GetPropertiesTestMethod7()
        {
            List<PropertyInfo> props = DataManager.GetProperties("/Project", "Choose");
            Assert.AreEqual(1, props.Count);
            Assert.AreEqual("Label", props[0].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props[0].Use);

        }

        #endregion

        #region GetTasks
        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetTasksTestMethod1()
        {
            DataManager.GetTasks(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetTasksTestMethod2()
        {
            DataManager.GetTasks("");
        }

        [TestMethod]
        public void GetTasksTestMethod3()
        {
            List<TaskInfo> tInfos =  DataManager.GetTasks("aaa");
            Assert.IsNull(tInfos);
        }

        [TestMethod]
        public void GetTasksTestMethod4()
        {
            List<TaskInfo> tInfos = DataManager.GetTasks("/");
            Assert.IsNotNull(tInfos);
            Assert.AreEqual(1, tInfos.Count);
            Assert.AreEqual("Project", tInfos[0].Name);

            List<PropertyInfo> props = tInfos[0].Properties;
            Assert.AreEqual(3, props.Count);
            Assert.AreEqual("DefaultTargets", props[0].Name);
            Assert.AreEqual("InitialTargets", props[1].Name);
            Assert.AreEqual("ToolsVersion", props[2].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props[1].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props[2].Use);

        }

        [TestMethod]
        public void GetTasksTestMethod5()
        {
            List<TaskInfo> tInfos = DataManager.GetTasks("/Project");
            Assert.IsNotNull(tInfos);
            Assert.AreEqual(9, tInfos.Count);
            Assert.AreEqual("Choose", tInfos[0].Name);
            Assert.AreEqual("Import", tInfos[1].Name);
            Assert.AreEqual("ImportGroup", tInfos[2].Name);
            Assert.AreEqual("ItemDefinitionGroup", tInfos[3].Name);
            Assert.AreEqual("ItemGroup", tInfos[4].Name);
            Assert.AreEqual("ProjectExtensions", tInfos[5].Name);
            Assert.AreEqual("PropertyGroup", tInfos[6].Name);
            Assert.AreEqual("Target", tInfos[7].Name);
            Assert.AreEqual("UsingTask", tInfos[8].Name);

            List<PropertyInfo> props0 = tInfos[0].Properties;
            Assert.AreEqual(1, props0.Count);
            Assert.AreEqual("Label", props0[0].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props0[0].Use);

            List<PropertyInfo> props1 = tInfos[1].Properties;
            Assert.AreEqual(3, props1.Count);
            Assert.AreEqual("Condition", props1[0].Name);
            Assert.AreEqual("Label", props1[1].Name);
            Assert.AreEqual("Project", props1[2].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props1[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props1[1].Use);
            Assert.AreEqual(XmlSchemaUse.Required, props1[2].Use);

            List<PropertyInfo> props2 = tInfos[2].Properties;
            Assert.AreEqual(2, props2.Count);
            Assert.AreEqual("Condition", props2[0].Name);
            Assert.AreEqual("Label", props2[1].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props2[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props2[1].Use);

            List<PropertyInfo> props3 = tInfos[3].Properties;
            Assert.AreEqual(2, props3.Count);
            Assert.AreEqual("Condition", props3[0].Name);
            Assert.AreEqual("Label", props3[1].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props3[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props3[1].Use);

            List<PropertyInfo> props4 = tInfos[4].Properties;
            Assert.AreEqual(2, props4.Count);
            Assert.AreEqual("Condition", props4[0].Name);
            Assert.AreEqual("Label", props4[1].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props4[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props4[1].Use);

            List<PropertyInfo> props5 = tInfos[5].Properties;
            Assert.AreEqual(0, props5.Count);

            List<PropertyInfo> props6 = tInfos[6].Properties;
            Assert.AreEqual(2, props6.Count);
            Assert.AreEqual("Condition", props6[0].Name);
            Assert.AreEqual("Label", props6[1].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props6[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props6[1].Use);

            List<PropertyInfo> props7 = tInfos[7].Properties;
            Assert.AreEqual(10, props7.Count);
            Assert.AreEqual("AfterTargets", props7[0].Name);
            Assert.AreEqual("BeforeTargets", props7[1].Name);
            Assert.AreEqual("Condition", props7[2].Name);
            Assert.AreEqual("DependsOnTargets", props7[3].Name);
            Assert.AreEqual("Inputs", props7[4].Name);
            Assert.AreEqual("KeepDuplicateOutputs", props7[5].Name);
            Assert.AreEqual("Label", props7[6].Name);
            Assert.AreEqual("Name", props7[7].Name);
            Assert.AreEqual("Outputs", props7[8].Name);
            Assert.AreEqual("Returns", props7[9].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[1].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[2].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[3].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[4].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[5].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[6].Use);
            Assert.AreEqual(XmlSchemaUse.Required, props7[7].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[8].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props7[9].Use);

            List<PropertyInfo> props8 = tInfos[8].Properties;
            Assert.AreEqual(7, props8.Count);
            Assert.AreEqual("Architecture", props8[0].Name);
            Assert.AreEqual("AssemblyFile", props8[1].Name);
            Assert.AreEqual("AssemblyName", props8[2].Name);
            Assert.AreEqual("Condition", props8[3].Name);
            Assert.AreEqual("Runtime", props8[4].Name);
            Assert.AreEqual("TaskFactory", props8[5].Name);
            Assert.AreEqual("TaskName", props8[6].Name);
            Assert.AreEqual(XmlSchemaUse.Optional, props8[0].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props8[1].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props8[2].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props8[3].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props8[4].Use);
            Assert.AreEqual(XmlSchemaUse.Optional, props8[5].Use);
            Assert.AreEqual(XmlSchemaUse.Required, props8[6].Use);

        }

        #endregion

    }
}
